"""
@author: xxw
@file: 爬取三创官网并下载图片.py
@time: 2023/5/31 19:39
"""
import re
import requests

sc = "https://www.sanchuangedu.cn/"

# 页面拉取函数
def load_data(url):
    try:
        res = requests.get(url)
        if res.status_code == 200:
            res.encoding = "utf-8"
            return res.text
        else:
            return lambda:print("拉取页面出错")
    except:
        print("拉取出错")

html = load_data(sc)

#获取图片网址
img = re.findall(r"(?<=<img src=).*(?= width)", html)
for i, v in enumerate(img):
    img[i] = sc + v

for img_url in img:
    # 发送 HTTP GET 请求，获取图片内容
    response = requests.get(img_url)

    # 从 URL 中提取出文件名
    filename = img_url.split("/")[-1]

    # 将图片内容写入本地文件
    with open(filename, "wb") as f:
        f.write(response.content)




